package algorithm

import java.lang.StringBuilder

object ChineseNum {
    val cnum = arrayOf("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖")
//    val unit = arrayOf("分", "角", "元", "拾", "佰", "仟", "万", "亿")
    val unit = mutableMapOf(
        0 to "元",
        1 to "拾",
        2 to "佰",
        3 to "仟",
        4 to "万",
    )
    fun convert(num: Double): String {

        val fsb = StringBuilder()
        val zsb = StringBuilder()
        val zlist = mutableListOf<String>()

        val zs = num.toInt()
        val xs = num - zs
        val hasFloat = zs - xs > 0

        if (hasFloat) {
            //0.003
            val cftp = (xs * 100).toInt()
            if (cftp > 0) {
                val j = cftp / 10
                val f = cftp % 10
                if (j > 0)
                    fsb.append("${cnum[j]}角")
                if (f > 0)
                    fsb.append("${cnum[f]}分")
            }
        }
        var z = zs
        if (z > 0) {
            if (!hasFloat) {
                zsb.append("元")
            }
            var i = 1
            while (z > 0) {
                val mod = z % 10
                z /= 10
                zlist.add(cnum[mod])
                i++
            }
            zlist.forEachIndexed { index, s ->
                if(index == 0)
                    zsb.append("元")
                zsb.append(s)
            }
        }
        return zlist.joinToString(",")
    }

    @JvmStatic
    fun main(args: Array<String>) {
        //123.34
        println(convert(2123000789.0))
    }
}